<?php

namespace common\models\user;

use Yii;
use common\helpers\Url;
use common\helpers\StringHelper;

/**
 * This is the model class for table "{{%user_authent}}".
 *
 * @property int $id
 * @property int $user_id 用户ID
 * @property string $realname 真实姓名
 * @property int $gender 性别
 * @property string $photo 人像照片
 * @property string $passport_front 证件正面照
 * @property string $passport_reverse 证件反面照
 * @property int $ict_id 证件类型
 * @property string $idcardno 证件号
 * @property string $idcardno_hash 证件号
 * @property string $birthday 出生日期
 * @property string $expiry_date 证件有效期
 * @property int $mark 审核失败原因
 * @property int $status 认证状态
 * @property int $created_at 认证时间
 * @property int $updated_at 最后更新时间
 */
class UserAuthent extends \common\db\ActiveRecord {

    use \common\traits\relation\UserData;

    /**
     * @const GENDERA_FEMALE 女
     * @const GENDERA_MALE 男
     */
    const GENDER_FEMALE = 0;
    const GENDER_MALE = 1;
    //
    const STATUS_DEFAULT = 0;
    const STATUS_AUDITING = 1;
    const STATUS_REFUSE = -1;
    const STATUS_ACTIVE = 10;

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return '{{%user_authent}}';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['user_id', 'ict_id', 'gender', 'status', 'created_at', 'updated_at'], 'integer'],
            [['realname', 'idcardno', 'idcardno_hash', 'photo', 'passport_front', 'passport_reverse'], 'required'],
            [['birthday', 'expiry_date'], 'safe'],
            [['realname'], 'string', 'max' => 50],
            ['idcardno_hash', 'string', 'max' => 255],
            ['mark', 'string', 'max' => 255],
            [['photo', 'passport_front', 'passport_reverse'], 'string', 'max' => 255],
            [['idcardno'], 'string', 'max' => 32],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'user_id' => '用户ID',
            'realname' => '真实姓名',
            'gender' => '性别',
            'photo' => '头像',
            'passport_front' => '头像',
            'passport_reverse' => '头像',
            'ict_id' => '证件类型',
            'idcardno' => '证件号',
            'idcardno_hash' => '证件号',
            'birthday' => '出生日期',
            'expiry_date' => '证件有效期',
            'mark' => '审核失败原因',
            'status' => '认证状态',
            'created_at' => '认证时间',
            'updated_at' => '最后更新时间',
        ];
    }

    public function checkUpdatePermission() {
        if ($this->isNewRecord || $this->status === static::STATUS_REFUSE) {
            return true;
        } elseif ($this->status === static::STATUS_ACTIVE) {
            return $this->dumpError('您已通过了实名认证，无需重新认证');
        } elseif ($this->status === static::STATUS_DEFAULT) {
            return $this->dumpError('您的认证信息正在审核中，不能修改');
        } else {
            return $this->dumpError('认证操作失败未知错误，可能您的账户信息异常');
        }
    }

    public function setApply() {
        $this->status = static::STATUS_DEFAULT;
    }

    /**
     * @inheritdoc
     */
    public function setIdcardno($idcardno) {
        if ($idcardno) {
            $this->ict_id = 1;
            $this->birthday = substr($idcardno, 6, 8);
            $this->gender = 2 - substr($idcardno, 16, 1) % 2;
            /* @var $security \common\base\Security */
            $security = Yii::$app->security;
            $this->idcardno = $security->generatePrivateKey($idcardno);
            $this->idcardno_hash = $security->encryptMaster($idcardno);
        }
    }

    /**
     * 屏蔽身份证号码
     * @inheritdoc
     */
    public function getIdCardNo($mask = true) {
        $idcardno = Yii::$app->security->decryptMaster($this->idcardno_hash);
        if ($mask !== false) {
            return StringHelper::maskChar($idcardno, 1, 1);
        }
        return $idcardno;
    }

    /**
     * 获取审核状态
     * @inheritdoc
     */
    public function getAuditStatus() {
        if ($this->status == static::STATUS_REFUSE) {
            return [
                'icon' => Url::attach('/assets/icons/authent/audit_refuse.png'),
                'text' => '审核未通过',
            ];
        } elseif ($this->status == static::STATUS_DEFAULT) {
            return [
                'icon' => Url::attach('/assets/icons/authent/auditing.png'),
                'text' => '审核中',
            ];
        } elseif ($this->status == static::STATUS_ACTIVE) {
            return [
                'icon' => Url::attach('/assets/icons/authent/allowed.png'),
                'text' => '认证通过',
            ];
        }
        return [
            'icon' => Url::attach('/assets/icons/authent/abnormal.png'),
            'text' => '信息异常',
        ];
    }

    public function getText() {
        if ($this->status == static::STATUS_DEFAULT) {
            return '认证审核中';
        } elseif ($this->status == static::STATUS_ACTIVE) {
            return '已认证';
        }
        return '审核失败';
    }

    public function getLink() {
        return Url::h5('authent');
    }

    public function fields() {
        return [
            'status',
            'text',
            'link',
        ];
    }

    /**
     * @inheritdoc
     */
    public static function findByIdcardno($idcardno) {
        /* @var $security \common\base\Security */
        $security = Yii::$app->security;
        $entra = $security->generatePrivateKey($idcardno);
        return static::find()->andWhere([
            'idcardno' => $entra
        ])->one();
    }

}
